import axios from 'axios';

export async function getCurrList() {
  try {
    const response = await axios('/api/curriculum/list');
    const result = response.data;
    return result.data;
  } catch (error) {
    console.error('Error fetching data:', error);
    return null;
  }
}

// 表名常量
export const TableName = {
  Articles: 1,
  Products: 2,
  Movies: 3,
  Comments: 4,
  Categories: 5
};

export async function getList(typeIndex = 1, articleid = 0, replyType = '文章') {
  try {
    let url = '/api/demo/getallitems?typeIndex=' + typeIndex;
    if (articleid) {
      url += '&articleid=' + articleid + '&replyType=' + replyType;
    }
    const response = await axios(url);
    return response.data;
  } catch (error) {
    console.error('Error fetching data:', error);
    return null;
  }
}

export async function getSingle(id, typeIndex = 1) {
  try {
    const response = await axios('/api/demo/getsingle?id=' + id + '&typeIndex=' + typeIndex);
    return response.data;
  } catch (error) {
    console.error('Error fetching data:', error);
    return null;
  }
}

export async function login(data) {
  try {
    const response = await axios.post('/api/demo/login', data);
    const result = response.data;
    if (result.errno == 0) {
      return result.data;
    }
    return null;
  } catch (error) {
    console.error('Error fetching data:', error);
    return null;
  }
}

export async function reg(data) {
  try {
    const response = await axios.post('/api/demo/reg', data);
    return response.data;
  } catch (error) {
    console.error('Error fetching data:', error);
    return false;
  }
}

export async function add(data) {
  try {
    const response = await axios.post('/api/demo/additem', data);
    const result = response.data;
    return result.errno == 0;
  } catch (error) {
    console.error('Error fetching data:', error);
    return false;
  }
}

export async function update(data) {
  try {
    const response = await axios.post('/api/todo/updateitem', data);
    const result = response.data;
    return result.errno == 0;
  } catch (error) {
    console.error('Error fetching data:', error);
    return false;
  }
}